package dao

import (
	"fmt"
	"gin/model"
	"gin/param"
	"gin/tool"
	"time"
)

type MemberDao struct {
	*tool.Orm
}

func (md *MemberDao) InsertCode(sms model.SmsCode) int64 {
	row, err := md.InsertOne(sms)
	if err != nil {
		fmt.Println("--------------数据插入失败---------------")
		return 0
	}
	return row
}

//验证手机号和验证码是否存在
func (md *MemberDao) ValidateSmsCode(mobile string, code string, status int) *model.SmsCode {
	var sms model.SmsCode
	if _, err := md.Where("mobile = ? AND code = ? AND status = ?", mobile, code, status).Desc("id").Get(&sms); err != nil {
		fmt.Println(err.Error())
		return nil
	}
	return &sms
}

// 用户注册
func (md *MemberDao) MemberRegister(member model.Member) int64 {
	row, err := md.Insert(member)
	if err != nil {
		fmt.Println("--------------用户注册数据插入失败---------------")
		return 0
	}
	return row
}

//账号密码登录
func (md *MemberDao) AccPassLogin(param param.AccPassLoginParam) *model.Member {
	var member model.Member
	if _, err := md.Where("(account = ? AND password = ?) OR (mobile = ? AND password = ?)", param.Account, param.Password, param.Account, param.Password).Get(&member); err != nil {
		fmt.Println("----账号密码查询------", err)
		return nil
	}
	return &member
}

//查询该手机号是否存在
func (md *MemberDao) MobileIsSet(param param.SmsLoginParam) *model.Member {
	var member model.Member
	if _, err := md.Where("mobile = ?", param.Mobile).Get(&member); err != nil {
		fmt.Println("----手机号是否存在----", err)
		return nil
	}
	return &member
}

//根据ID查询出来用户信息
func (md *MemberDao) GetMemberInfo(id int) *model.Member {
	var member model.Member
	if _, err := md.Where("id = ?", id).Get(&member); err != nil {
		fmt.Println("----查询出来用户信息----", err)
		return nil
	}
	return &member
}

//修改用户数据
func (md *MemberDao) MemberEdit(member model.Member) *model.Member {
	member.Utime = time.Now().Unix()
	_, err := md.Table("member").Where("id = ?", member.Id).Update(member)
	if err != nil {
		fmt.Println("---用户数据修改失败---", err)
	}
	return &member
}
